<?php

/**
 * Article filter form base class.
 *
 * @package    myarticles
 * @subpackage filter
 * @author     Apul Gupta<apulmca2k4@gmail.com>
 */
abstract class BaseArticleFormFilter extends BaseFormFilterPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'title'                 => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'url_slug'              => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'meta_title'            => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'meta_keyword'          => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'meta_description'      => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'user_id'               => new sfWidgetFormPropelChoice(array('model' => 'User', 'add_empty' => true)),
      'article_views'         => new sfWidgetFormFilterInput(),
      'total_rating'          => new sfWidgetFormFilterInput(),
      'total_user_rated'      => new sfWidgetFormFilterInput(),
      'content'               => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'author_speech'         => new sfWidgetFormFilterInput(),
      'article_summary'       => new sfWidgetFormFilterInput(),
      'word_count'            => new sfWidgetFormFilterInput(),
      'article_source'        => new sfWidgetFormFilterInput(),
      'created_at'            => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'updated_at'            => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'is_active'             => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'article_category_list' => new sfWidgetFormPropelChoice(array('model' => 'Category', 'add_empty' => true)),
    ));

    $this->setValidators(array(
      'title'                 => new sfValidatorPass(array('required' => false)),
      'url_slug'              => new sfValidatorPass(array('required' => false)),
      'meta_title'            => new sfValidatorPass(array('required' => false)),
      'meta_keyword'          => new sfValidatorPass(array('required' => false)),
      'meta_description'      => new sfValidatorPass(array('required' => false)),
      'user_id'               => new sfValidatorPropelChoice(array('required' => false, 'model' => 'User', 'column' => 'id')),
      'article_views'         => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'total_rating'          => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'total_user_rated'      => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'content'               => new sfValidatorPass(array('required' => false)),
      'author_speech'         => new sfValidatorPass(array('required' => false)),
      'article_summary'       => new sfValidatorPass(array('required' => false)),
      'word_count'            => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'article_source'        => new sfValidatorPass(array('required' => false)),
      'created_at'            => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'updated_at'            => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'is_active'             => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'article_category_list' => new sfValidatorPropelChoice(array('model' => 'Category', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('article_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function addArticleCategoryListColumnCriteria(Criteria $criteria, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $criteria->addJoin(ArticleCategoryPeer::ARTICLE_ID, ArticlePeer::ID);

    $value = array_pop($values);
    $criterion = $criteria->getNewCriterion(ArticleCategoryPeer::CATEGORY_ID, $value);

    foreach ($values as $value)
    {
      $criterion->addOr($criteria->getNewCriterion(ArticleCategoryPeer::CATEGORY_ID, $value));
    }

    $criteria->add($criterion);
  }

  public function getModelName()
  {
    return 'Article';
  }

  public function getFields()
  {
    return array(
      'id'                    => 'Number',
      'title'                 => 'Text',
      'url_slug'              => 'Text',
      'meta_title'            => 'Text',
      'meta_keyword'          => 'Text',
      'meta_description'      => 'Text',
      'user_id'               => 'ForeignKey',
      'article_views'         => 'Number',
      'total_rating'          => 'Number',
      'total_user_rated'      => 'Number',
      'content'               => 'Text',
      'author_speech'         => 'Text',
      'article_summary'       => 'Text',
      'word_count'            => 'Number',
      'article_source'        => 'Text',
      'created_at'            => 'Date',
      'updated_at'            => 'Date',
      'is_active'             => 'Boolean',
      'article_category_list' => 'ManyKey',
    );
  }
}
